package com.example.smartcity.ui.frg

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity.R
import com.example.smartcity.adapter.ProductCategoryAdapter
import com.example.smartcity.adapter.ProductListVAdapter
import com.example.smartcity.app.App
import com.example.smartcity.http.ProductCategoryResp
import com.example.smartcity.http.ProductResp
import kotlinx.android.synthetic.main.fragment_tof_seller_detail_tab1.*

/**
 * @Author: Tailyou(祝文飞)
 * @CreateDate: 2021/10/7 14:53
 * @Description: 店铺-点菜
 */
class TOFOFrg : Fragment(R.layout.fragment_tof_seller_detail_tab1) {

    companion object {
        fun newInstance(id: Int): TOFOFrg {
            var tofoFrg = TOFOFrg()
            var args = Bundle()
            args.putInt("id", id)
            tofoFrg.arguments = args
            return tofoFrg
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loadMenu()
    }

    // 读取左侧menu数据
    private fun loadMenu() {
        var id = arguments?.get("id")
        App.fetchData<ProductCategoryResp>(
            "/prod-api/api/takeout/category/list?sellerId=$id",
            ProductCategoryResp::class.java
        ) {
            val linearLayoutManager = LinearLayoutManager(context)
            linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
            rvMenu.layoutManager = linearLayoutManager
            rvMenu.adapter = ProductCategoryAdapter(it.data,
                object : ProductCategoryAdapter.OnItemClickListener {
                    override fun onClick(position: Int) {
                        ProductCategoryAdapter.setPosition(position)
                        rvMenu.adapter?.notifyDataSetChanged()
                        var categoryId = it.data[position].id
                        loadContent(categoryId, id)
                    }
                })
            //默认选中左侧菜单项的第一个，同时加载右侧内容数据
            ProductCategoryAdapter.setPosition(0)
            rvMenu.adapter?.notifyDataSetChanged()
            loadContent(it.data[0].id, id)
        }
    }

    // 读取指定分类下的菜品
    private fun loadContent(categoryId: Int, id: Any?) {
        App.fetchData<ProductResp>(
            "/prod-api/api/takeout/product/list?categoryId=$categoryId&sellerId=$id",
            ProductResp::class.java
        ) {
            val linearLayoutManager = LinearLayoutManager(context)
            linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
            rvContent.layoutManager = linearLayoutManager
            rvContent.adapter = ProductListVAdapter(it.data,
                object : ProductListVAdapter.OnItemClickListener {
                    override fun onClick(position: Int) {

                    }
                })
        }
    }

}